//  用户管理系统
const Controller = require('egg').Controller;
const sqlConfig = require('../util/sqlConfig')
const err = require('../util/err')
const moment = require('moment')
// let code 


class LoginController extends Controller {
    // 后台管理系统
    async manageIndex() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.srcs&&r_body.src){
            await this.app.mysql.update('banner', {
                srcs: r_body.srcs,
                src: r_body.src,
            },{
                where: {
                    id:r_body.id
                    // tel: r_body.tel
                }
            })
            ctx.body = {
                code: 200,
                msg: '修改成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }
    async manageConcat() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        await this.app.mysql.update('concat', {
            txt3: r_body.txt3,
            txt4: r_body.txt4,
            txt5: r_body.txt5,
            txt6: r_body.txt6
        },{
            where: {
                uSchoolId:r_body.uSchoolId
            }
        })
        ctx.body = {
            code: 200,
            msg: '修改成功',
            data: {}
        }
    }

    // 活动查询
    async activeGet() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.uSchoolId){
            let fil = {
                where: { uSchoolId: r_body.uSchoolId,type:r_body.type}, 
                orders: [['id','desc']], 
              }
            const data = await app.mysql.select('active',fil);
            ctx.body = {
                code: 200,
                msg: '查询成功',
                data: data
            }
        }else{
            ctx.body = err.err1
        }
    }
    // 活动删除
    async activeDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.id){
            await app.mysql.delete('active', {
                id: r_body.id,
            });
            ctx.body = {
                code: 200,
                msg: '删除成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }
    // 活动新增
    async activeAdd() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.manageId){
            await app.mysql.insert('active', { ...r_body,publishTime:moment(new Date()).format('YYYY-MM-DD HH:mm:ss') })
            ctx.body = {
                code: 200,
                msg: '新增成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }
    // 活动编辑
    async activeUpdate() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.id){
            // console.log( {..._body})
            const {src,title,intro,activeTime,address,content} = r_body
            const row = {
                src,title,intro,activeTime,address,content
            }
            await app.mysql.update('active', row,{
                where: {
                    id: r_body.id
                }
            })
            ctx.body = {
                code: 200,
                msg: '编辑成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }

    // 商户入住查询
    async bussinessGet() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const {uSchoolId} = r_body
        const data = await app.mysql.select('info', { // 搜索 post 表
            where:{
                uSchoolId
            },
            orders: [['id','desc']], // 排序方式
        });
        ctx.body = {
            code: 200,
            msg: '查询成功',
            data: data
        }
    }

    //   u选查询 
    async uniGet() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.manageId){
            let fil
            if(r_body.aType!==0){
                fil = {
                    where: { manageId: r_body.manageId}, 
                    orders: [['id','desc']], 
                  }
            }else{
                fil = {
                    orders: [['id','desc']], 
                }
            }
            const data = await app.mysql.select('business', fil);
            ctx.body = {
                code: 200,
                msg: '查询成功',
                data: data
            }
        }else{
            ctx.body = err.err1
        }
    }

    // u选新增
    async uniAdd() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.manageId){
            await app.mysql.insert('business', { ...r_body,publishTime:moment(new Date()).format('YYYY-MM-DD HH:mm:ss') })
            ctx.body = {
                code: 200,
                msg: '新增成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }

    // u选编辑
    async uniUpdate() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.id){
            // console.log( {..._body})
            const {title,listSrc,detailSrcs,intro,businessTime,tel,address,endTime,content,card1,card2,card3,card4} = r_body
            const row = {
                title,listSrc,detailSrcs,intro,businessTime,tel,address,endTime,content,card1,card2,card3,card4
            }
            await app.mysql.update('business', row,{
                where: {
                    id: r_body.id
                }
            })
            ctx.body = {
                code: 200,
                msg: '编辑成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }
    // u选删除
    async uniDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        if(r_body.id){
            await app.mysql.delete('business', {
                id: r_body.id,
            });
            ctx.body = {
                code: 200,
                msg: '删除成功',
                data: {}
            }
        }else{
            ctx.body = err.err1
        }
    }
    

}



module.exports = LoginController;